package com.pet.server.bo.system;

import java.util.Date;

import com.pet.framework.util.HttpHelp;
import com.pet.framework.util.MD5Util;
import com.pet.server.bo.common.CommonService;
import com.pet.server.dao.system.SystemDao;
import com.pet.server.po.SysUser;
import com.pet.system.constants.ATTSystem;
import com.pet.system.util.SessionHelper;
public class SystemService extends CommonService {
    private SystemDao systemDao;
	
	
	public SystemDao getSystemDao() {
		return systemDao;
	}


	public void setSystemDao(SystemDao systemDao) {
		this.systemDao = systemDao;
	}


	public SysUser findUserByName(String userName){
		return systemDao.findUserByName(userName);
	}
	public void updateUserForLogin(SysUser user){
		updateUserLogin(user);//更新登录用户
		SessionHelper.set(ATTSystem.SESSION_USER, user);//设置session
    }
	private void updateUserLogin(SysUser user){
		// 记录次数修改
		Integer times =  user.getLoginTime() == null ? 0 : user.getLoginTime();
		times++;
		user.setLoginTime(times);
		// 记录登陆时间
		user.setLastDate(new Date());
		// 记录登陆ip
		//String ip = HttpHelp.getIp();
		HttpHelp.addCookie(null, ATTSystem.DAYS_TIMES, "/", ATTSystem.COOKIE_NAME, user.getUserName());//设置cookie
		updateObject(user);
	}
	public void updatePwd(SysUser u,String password){
		u.setPassword(new MD5Util().getMD5ofStr(password));
		updateObject(u);
	}
	public void logOut(){
		SessionHelper.remove(ATTSystem.SESSION_USER);
		HttpHelp.removeCookie(ATTSystem.COOKIE_NAME);
	}
	
	
}
